package com.crazy.crazytransactionservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.constant.TransactionStatusConstant;
import com.crazy.crazycommon.constant.TransactionTypeConstant;
import com.crazy.crazycommon.exception.BusinessException;
import com.crazy.crazymodel.model.dto.trade.TradeRequest;
import com.crazy.crazymodel.model.entity.Transaction;
import com.crazy.crazymodel.model.vo.OrderCancelVO;
import com.crazy.crazyserviceclient.service.BusinessFeignClient;
import com.crazy.crazytransactionservice.mapper.TransactionMapper;
import com.crazy.crazytransactionservice.service.CancelService;
import com.crazy.crazytransactionservice.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Date: 2024-05-07-15:00
 * @Author: Zheng Zeliang
 * 撤单Service实现类
 */
@Service
@Slf4j
public class CancelServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements CancelService {
    @Resource
    private TransactionMapper transactionMapper;

    @Resource
    private BusinessFeignClient businessFeignClient;

    @Autowired
    private PositionService positionService;

    @Override
    public OrderCancelVO cancel(int orderId) {
        Transaction transaction = transactionMapper.selectById(orderId);
        if (transaction == null) {
            log.info("订单不存在：{}",orderId);
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "订单不存在");
        }
        int customerID = transaction.getCustomerID();
        int productID = transaction.getProductID();

        int transactionId = transaction.getTransactionId();
        int transactionType = transaction.getTransactionType();
        BigDecimal amount = transaction.getAmount();
        BigDecimal share = transaction.getShare();
        int status = transaction.getStatus();
        String bankCardNumber = transaction.getBankCardNumber();
        //判断订单状态，如果是已确认或已取消，则不能撤单
        if (status != TransactionStatusConstant.PENDING) {
            log.info("订单无法撤单:{}", transactionId);
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "无法撤单");
        }
        // 返回前端视图
        OrderCancelVO orderCancelVO = new OrderCancelVO();
        orderCancelVO.setTransactionId(transactionId);
        Date cancelTime = new Date(System.currentTimeMillis());
        orderCancelVO.setCancelTime(cancelTime);
        // 如果撤单的是申购订单
        if (transactionType == TransactionTypeConstant.SUBSCRIBE) {
            orderCancelVO.setAmount(amount);
            TradeRequest tradeRequest = new TradeRequest();
            tradeRequest.setBankCardId(bankCardNumber);
            tradeRequest.setAmount(amount);
            businessFeignClient.freezeBalanceChancel(tradeRequest);
        }
        // 如果撤单的是赎回订单
        else if (transactionType == TransactionTypeConstant.REDEEM){
            orderCancelVO.setShare(share);
            positionService.cancelFreeze(customerID,productID,share,bankCardNumber);
        }
        //更新交易库表
        Transaction updateTransaction = new Transaction();
        updateTransaction.setTransactionId(transactionId);
        updateTransaction.setCancelTime(cancelTime);
        updateTransaction.setStatus(TransactionStatusConstant.CANCELED);
        transactionMapper.updateById(updateTransaction);

        return orderCancelVO;
    }
}
